package com.gsn.chooseChannel;

import java.io.File;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenCallback.EventType;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.gsn.common.SettingGroup;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.desktop.engine.DownloadDesktop;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.engine.utility.IUtility.IPictureDownloadListener;
import com.gsn.name.NameEdit;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ChooseChannelLayer extends GsnLayer implements ClickListener, IPictureDownloadListener {

	PortalTexture portalTexture;
	GsnImageButton putButton;
	GsnImageButton inviteButton;
	GsnImageButton shopButton;
	GsnImageButton helpButton;
	GsnImageButton quitButton;

	GsnImageButton addButton;
	GsnImageButton subButton;

	ListChannel listChannel;
	Label labelGold;
	Label labelName;
	Label labelXu;
	ListShopGold listShopGold;
	FlickScrollPane scrollShopGold;
	Image bgQuickShop;
	Image bgChooseChannel;

	Image avatar;
	private File avatarFile;

	public ChooseChannelLayer(float width, float height) {
		super(width, height);
		portalTexture = PortalTexture.inst();
		init();
	}

	public void init() {

		FontTexture fontMedium = FontTexture.inst();

		Image bg = new Image(portalTexture.bgPortal);
		bg.width = width;
		bg.height = height;
		addActor(bg);

		Image bgAvatar = new Image(portalTexture.bgAvatar);
		portalTexture.scaleActor(bgAvatar);
		addActor(bgAvatar);
		bgAvatar.y = height * 0.97f - bgAvatar.height;

		Image logo = new Image(portalTexture.xitoLogo);
		portalTexture.scaleActor(logo);
		addActor(logo);
		logo.x = (width - logo.width) * 0.5f;
		logo.y = height * 0.97f - logo.height;

		putButton = new GsnImageButton(portalTexture.putButton, portalTexture.putButtonPress);
		portalTexture.scaleActor(putButton);
		float pad = (width - 4 * putButton.getRealWidth()) / 5;
		addActor(putButton);
		putButton.setClickListener(this);
		putButton.x = pad;

		inviteButton = new GsnImageButton(portalTexture.inviteButton, portalTexture.inviteButtonPress);
		portalTexture.scaleActor(inviteButton);
		addActor(inviteButton);
		inviteButton.setClickListener(this);
		inviteButton.x = putButton.x + pad + putButton.getRealWidth();

		shopButton = new GsnImageButton(portalTexture.shopButton, portalTexture.shopButtonPress);
		portalTexture.scaleActor(shopButton);
		addActor(shopButton);
		shopButton.setClickListener(this);
		shopButton.x = inviteButton.x + pad + inviteButton.getRealWidth();

		helpButton = new GsnImageButton(portalTexture.helpButton, portalTexture.helpButtonPress);
		portalTexture.scaleActor(helpButton);
		addActor(helpButton);
		helpButton.setClickListener(this);
		helpButton.x = shopButton.x + pad + shopButton.getRealWidth();

		Image line = new Image(portalTexture.iconLine);
		portalTexture.scaleActor(line);
		addActor(line);
		line.x = width - line.width;
		line.y = height * 0.97f - line.height;

		quitButton = new GsnImageButton(portalTexture.quitButton, portalTexture.quitButtonPress);
		portalTexture.scaleActor(quitButton);
		addActor(quitButton);
		quitButton.setClickListener(this);
		quitButton.x = width - quitButton.getRealWidth() * 1.2f;
		quitButton.y = line.y + (line.height - quitButton.getRealHeight()) / 2;

		labelGold = new Label("1000", new Label.LabelStyle(fontMedium.fontMedium, new Color(1, 1, 1, 1)));
		labelName = new Label("Cường cận", new Label.LabelStyle(fontMedium.fontMedium, new Color(1, 1, 1, 1)));
		labelXu = new Label("1000", new Label.LabelStyle(fontMedium.fontMedium, new Color(1, 1, 1, 1)));

		pad = (bgAvatar.height - 3 * labelGold.getTextBounds().height) / 4;

		addActor(labelXu);

		Image iconXu = new Image(portalTexture.iconXuSmall);
		portalTexture.scaleActor(iconXu);
		addActor(iconXu);
		iconXu.x = bgAvatar.x + bgAvatar.width * 0.38f;
		iconXu.y = bgAvatar.y + pad;

		labelXu.x = iconXu.x + iconXu.width * 1.25f;
		labelXu.y = bgAvatar.y + pad;

		Image iconGold = new Image(portalTexture.iconGoldSmall);
		portalTexture.scaleActor(iconGold);
		addActor(iconGold);
		iconGold.x = bgAvatar.x + bgAvatar.width * 0.38f;
		iconGold.y = labelXu.y + pad + labelXu.getTextBounds().height;

		addActor(labelGold);
		labelGold.x = iconGold.x + iconGold.width * 1.25f;
		labelGold.y = iconGold.y;

		addActor(labelName);
		labelName.x = labelXu.x;
		labelName.y = labelGold.y + pad + labelName.getTextBounds().height;

		bgChooseChannel = new Image(portalTexture.bgChooseChannel);
		portalTexture.scaleActor(bgChooseChannel);
		addActor(bgChooseChannel);

		bgQuickShop = new Image(portalTexture.bgQuickShop);
		portalTexture.scaleActor(bgQuickShop);
		addActor(bgQuickShop);

		pad = (width - bgQuickShop.width - bgChooseChannel.width) / 3;
		bgChooseChannel.x = pad;
		bgChooseChannel.y = (bgAvatar.y - putButton.getRealHeight() - putButton.y - bgChooseChannel.height) / 2 + putButton.y + putButton.getRealHeight();
		bgQuickShop.x = bgChooseChannel.x + bgChooseChannel.width + pad;
		bgQuickShop.y = bgChooseChannel.y;

		Label labelQuickShop = new Label("Cửa hàng", new Label.LabelStyle(fontMedium.fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelQuickShop);
		labelQuickShop.x = bgQuickShop.x + (bgQuickShop.width - labelQuickShop.getTextBounds().width) / 2;
		labelQuickShop.y = bgQuickShop.y + bgQuickShop.height - labelQuickShop.getTextBounds().height * 1.4f;

		listChannel = new ListChannel();
		FlickScrollPane scrollPane = new FlickScrollPane(listChannel);
		scrollPane.setupOverscroll(30, 50, 100);
		scrollPane.width = listChannel.width;
		scrollPane.height = bgChooseChannel.height * 0.88f;
		addActor(scrollPane);
		scrollPane.y = bgChooseChannel.y + bgChooseChannel.height * 0.02f;
		scrollPane.x = bgChooseChannel.x + (bgChooseChannel.width - listChannel.width) / 2;

		listShopGold = new ListShopGold(this);
		scrollShopGold = new FlickScrollPane(listShopGold);
		// listShopGold.scroll = scrollShopGold;
		scrollShopGold.setupOverscroll(30, 50, 100);
		addActor(scrollShopGold);
		Image image = new Image(portalTexture.changeGoldButton);
		portalTexture.scaleActor(image);
		scrollShopGold.width = image.width;
		scrollShopGold.height = bgQuickShop.height * 0.88f;

		scrollShopGold.y = bgQuickShop.y + bgQuickShop.height * 0.02f;
		scrollShopGold.x = bgQuickShop.x + (bgQuickShop.width - scrollShopGold.width) / 2;

		UtilityGettor.inst().makeTextFieldWithScreenCoordinate(this, PortalTexture.inst().skin.getStyle(TextField.TextFieldStyle.class), NameEdit.GOLD, "", 20, 30, 20, width / 3);
		UtilityGettor.inst().makeTextFieldWithScreenCoordinate(this, PortalTexture.inst().skin.getStyle(TextField.TextFieldStyle.class), NameEdit.XU, "", 20, 30, 20, width / 3 + 30f);

		addButton = new GsnImageButton(portalTexture.settingButton, portalTexture.settingButtonPress);
		portalTexture.scaleActor(addButton);
		addActor(addButton);
		addButton.setClickListener(this);
		addButton.x = width / 3 + 60;
		addButton.y = height - 30 - addButton.getRealHeight();

		subButton = new GsnImageButton(portalTexture.settingButton, portalTexture.settingButtonPress);
		portalTexture.scaleActor(subButton);
		addActor(subButton);
		subButton.setClickListener(this);
		subButton.x = addButton.x + addButton.getRealWidth();
		subButton.y = height - 30 - subButton.getRealHeight();

		avatar = new Image();
		addActor(avatar);

		avatar.width = avatar.height = bgAvatar.height * 0.95f;
		avatar.x = bgAvatar.height * 0.025f;
		avatar.y = bgAvatar.y + bgAvatar.height * 0.025f;

		updateWhenEnterGame();

	}

	@Override
	public boolean keyDown(int keycode) {

		return super.keyDown(keycode);
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub

		if (!MyPoker.game.statePortal.equals(StatePortal.CHOOSE_CHANNEL))
			return;
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
		if (actor == inviteButton) {
			PokerSound.playClick();
			UtilityGettor.inst().inviteFriend();
		} else if (actor == putButton) {
			PokerSound.playClick();

			((ChooseChannelScreen) screen).showCommingDialog();

		} else if (actor == helpButton) {
			PokerSound.playClick();

			((ChooseChannelScreen) screen).showHelpDialog();

		} else if (actor == inviteButton) {
			PokerSound.playClick();
		} else if (actor == quitButton) {
			PokerSound.playClick();
			MyPoker.game.setChooseGameScreen();
			if (MyPoker.client != null)
				MyPoker.client.disconnect();
		} else if (actor == shopButton) {
			PokerSound.playClick();
			((ChooseChannelScreen) screen).showShopGold();
		} else if (actor == addButton) {
			PokerSound.playClick();
			long numGold, numXu;
			try {
				numGold = Math.abs(Long.valueOf(UtilityGettor.inst().getString(NameEdit.GOLD)));
			} catch (Exception e) {
				// TODO: handle exception
				numGold = 0;
			}

			try {
				numXu = Math.abs(Long.valueOf(UtilityGettor.inst().getString(NameEdit.XU)));
			} catch (Exception e) {
				// TODO: handle exception
				numXu = 0;
			}
			MyPoker.client.send(PacketFactory.createCheatMoney(numGold, numXu));
		} else if (actor == subButton) {
			long numGold;
			try {
				numGold = -Math.abs(Long.valueOf(Long.valueOf(UtilityGettor.inst().getString(NameEdit.GOLD))));
			} catch (Exception e) {
				// TODO: handle exception
				numGold = 0;
			}
			MyPoker.client.send(PacketFactory.createCheatMoney(numGold, 0));
		}

	}

	public void updateInfoUser() {
		labelGold.setText(ActorUtility.formatNumber(UserData.gold));
		labelXu.setText(ActorUtility.formatNumber(UserData.xu));
		labelName.setText(ActorUtility.shortString(14, UserData.name));
		listChannel.updateChannel();
	}

	public void setAvatar() {
		UtilityGettor.inst().downloadPictureAsync("me", UserData.avatar, 64, 64, UserData.name + ".png", this);
	}

	public void updateWhenEnterGame() {
		listChannel.updateChannel();
		listShopGold.init();
		updateInfoUser();
		setAvatar();
		if (UserData.gold < ConfigModel.beanNeedSupport && UserData.numSupport > 0 && UserData.giftIndex <= 0) {
			MyPoker.client.send(PacketFactory.getSupportBean());
		}
	}

	public void showParticle(Actor actor, float x, float y) {
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
	}

	@Override
	public void onError(Exception e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFinishLoading(String id, File outFile) {
		// TODO Auto-generated method stub
		avatarFile = outFile;

		MyPoker.game.runInGLThread(new TweenCallback() {

			@Override
			public void onEvent(EventType eventType, BaseTween source) {
				try {
					Texture texture = new Texture(new FileHandle(avatarFile));
					TextureRegion region = new TextureRegion(texture);
					avatar.setRegion(region);
					avatarFile = null;
					// Image a = new Image(region);
					// addActor(a);
				} catch (Exception e) {
					avatarFile.delete();
					e.printStackTrace();
				} finally {
					avatarFile = null;
				}

			}
		}, 0);
	}
}
